package com.zhouchaoqun.web.api;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.zhouchaoqun.web.easyexcel.ExpressBillExccelReader;
import com.zhouchaoqun.web.easyexcel.ExpressBillExcelListener;
import com.zhouchaoqun.web.easyexcel.ExpressBillModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @author chaoqunzhou
 * @date 2019-08-05 19:58
 */
@RestController
@Slf4j
public class DemoApi {

    @GetMapping("hello")
    public String hello() {
        return "hello";
    }


    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file,
                                       @RequestParam("siteCode") String siteCode,
                                       @RequestParam("expressCompany") String expressCompany,
                                       @RequestParam("billingMonth") String billingMonth,
                                       @RequestParam("override") boolean override) {

        log.info("upload start, siteCode:{}, expressCompany:{}, billingMonth:{}, override:{}",
                siteCode, expressCompany, billingMonth, override);

        CountDownLatch latch = new CountDownLatch(1);
        ExpressBillExcelListener excelListener = new ExpressBillExcelListener(latch);
        ExpressBillExccelReader.read(file, 1, excelListener);
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(excelListener.getHeadMap());

        return "success";
    }


    @PostMapping("/upload1")
    public String upload1(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        Map<String, String> headerAlias = reader.getHeaderAlias();
        log.info("header:{}", headerAlias);
        List<List<Object>> headerList = reader.read(0, 0);
        log.info("first row:{}", headerList.get(0));


        List<ExpressBillModel> modelList = new LinkedList<>();
        ExcelUtil.readBySax(file.getInputStream(), 0, new RowHandler() {
            @Override
            public void handle(int sheetIndex, int rowIndex, List<Object> rowList) {
                if (rowIndex == 0) {
                    return;
                }
                modelList.add(convert(rowList));
            }

            public String parseString(Object o) {
                return o == null || "".equals(o) ? null : o.toString().trim();
            }

            private ExpressBillModel convert(List<Object> rowList) {
                ExpressBillModel model = new ExpressBillModel();
                model.setSequence(parseString(rowList.get(0)));
                model.setDate(parseString(rowList.get(1)));
                model.setExpressNumber(parseString(rowList.get(2)));
                model.setSender(parseString(rowList.get(3)));
                model.setReceiver(parseString(rowList.get(4)));
                model.setWeight(parseString(rowList.get(5)));
                model.setAddedValue(parseString(rowList.get(6)));
                model.setProductType(parseString(rowList.get(7)));
                model.setHandler(parseString(rowList.get(8)));
                model.setHandlerMis(parseString(rowList.get(9)));
                model.setHandlerPhone(parseString(rowList.get(10)));
                model.setPaymentMethod(parseString(rowList.get(11)));
                model.setOriginalCost(parseString(rowList.get(12)));
                model.setDiscount(parseString(rowList.get(13)));

                String o = parseString(rowList.get(14));
                if (o != null) {
                    model.setActualCost(BigDecimal.valueOf(Double.valueOf(o)));
                }
                model.setSendingContent(parseString(rowList.get(15)));
                return model;
            }
        });
        log.info("=======");
        log.info("dataList:{}", modelList);
//        ExcelUtil.readBySax(inputStream, 0, new RowHandler() {
//            @Override
//            public void handle(int sheetIndex, int rowIndex, List<Object> rowList) {
//                log.info("sheet:{}, rowIndex:{}, rowList:{}", sheetIndex, rowIndex, rowList);
//            }
//        });
        return "success";
    }
}
